package com.lanou.elh.sysmanage.system_member.service.impl;

import com.lanou.elh.sysmanage.system_member.entity.Tb_member;
import com.lanou.elh.sysmanage.system_member.mapper.MemberMapper;
import com.lanou.elh.sysmanage.system_member.service.MemberShowAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemberShowAllImpl implements MemberShowAll {

    @Autowired
    private MemberMapper memberMapper;

    public Integer countMember() {

        return memberMapper.countMapper();
    }

    public List<Tb_member> showMember(Tb_member tbMember) {
        List<Tb_member> list = memberMapper.showMapper(tbMember);
        return list;
    }

    @Override
    public void stopById(Tb_member tbMember) {
        memberMapper.stopById(tbMember);
    }

    @Override
    public void startById(Tb_member tbMember) {
        memberMapper.startById(tbMember);
    }

    @Override
    public void remove(Long id) {
        memberMapper.removeById(id);
    }

    @Override
    public Integer removeCountMember() {
        return memberMapper.removeCountMapper();
    }

    @Override
    public void restart(Long id) {
        memberMapper.restartById(id);
    }

    @Override
    public void delete(Long id) {
        memberMapper.deleteById(id);
    }

    @Override
    public Tb_member memberByUsername(String username) {
        List<Tb_member> list ;
        try {
            list = memberMapper.selectByUsername(username);
        }catch (Exception e){
            throw new RuntimeException("用户名获取失败");
        }
        if (!list.isEmpty()){
            list.get(0).setPassword("");
            return list.get(0);
        }
        return null;
    }

    @Override
    public Tb_member memberByPhone(String phone) {
        List<Tb_member> list;
        try {
            list = memberMapper.selectByPhone(phone);
        }catch (Exception e){
            throw new RuntimeException("电话获取失败");
        }
        if (!list.isEmpty()){
            list.get(0).setPassword("");
            return list.get(0);
        }
        return null;
    }

    @Override
    public Tb_member memberByEmail(String email) {
        List<Tb_member> list;
        try {
            list = memberMapper.selectByEmail(email);
        }catch (Exception e){
            throw new RuntimeException("邮箱获取失败");
        }
        if (!list.isEmpty()){
            list.get(0).setPassword("");
            return list.get(0);
        }
        return null;
    }

    @Override
    public void memberRegister(Tb_member tbMember) {

        if (memberByUsername(tbMember.getUsername())!=null){
            throw new RuntimeException("用户名重复");
        }
        if (memberByPhone(tbMember.getPhone())!=null){
            throw new RuntimeException("电话重复");
        }
        if (memberByEmail(tbMember.getEmail())!=null){
            throw new RuntimeException("邮箱重复");
        }

        tbMember.setState(1);
        tbMember.setCreated(new Date());
        tbMember.setUpdated(new Date());

        if (memberMapper.memberRegister(tbMember)!=1){
            throw new RuntimeException("添加用户失败");
        }

    }

    @Override
    public void memberUpdate(Tb_member tbMember) {
        if (memberByUsername(tbMember.getUsername())!=null){
            throw new RuntimeException("用户名重复");
        }
        if (memberByPhone(tbMember.getPhone())!=null){
            throw new RuntimeException("电话重复");
        }
        if (memberByEmail(tbMember.getEmail())!=null){
            throw new RuntimeException("邮箱重复");
        }

        tbMember.setUpdated(new Date());

        if (memberMapper.memberUpdate(tbMember)!=1){
            throw new RuntimeException("修改失败");
        }
    }

    @Override
    public void memberChangPass(Tb_member tbMember) {
        tbMember.setUpdated(new Date());
        if (memberMapper.memberUpdate(tbMember)!=1){
            throw new RuntimeException("修改密码失败");
        }
    }
}
